const Request = require("../../utils/util");//导入模块
Page({
  /**
   * 页面的初始数据
   */
  data: {
    contractType: ['直签合同', '定金合同'],
    parameter: {
      customerId: 0, //客户id
      contractNo: '',//合同编号
      contractType: 0,//合同类型
      contractProduct: '',//合同产品
      depositPrice: '', //定金价格
      contractPrice: '',//合同价格
      realityPrice: '',//实际价格
    }
  },

  /*/////////////////////数据绑定//////////////////////////// */
  NoValue: function (e) {
    let r = "parameter.contractNo";
    this.setData({
      [r]: e.detail.value
    })
  },
  contractProductValue: function (e) {
    let r = "parameter.contractProduct";
    this.setData({
      [r]: e.detail.value
    })
  },
  depositPriceValue: function (e) {
    let r = "parameter.depositPrice";
    this.setData({
      [r]: e.detail.value
    })
  },
  contractPriceValue: function (e) {
    let r = "parameter.contractPrice";
    this.setData({
      [r]: e.detail.value
    })
  },
  realityPriceValue: function (e) {
    let r = "parameter.realityPrice";
    this.setData({
      [r]: e.detail.value
    })
  },

  bindTypeChange: function () {
    let c = "parameter.contractType";
    this.setData({
      [c]: e.detail.value
    })
  },

  ///////////////////////事件///////////////////////
  initData: function () {
    let _this = this;
    wx.showLoading({
      title: '合同初始化',
      mask: true,
    })
    let p = {
      id: this.data.parameter.customerId
    }
    Request.get('emCustomer/getContractById', p).then(res => {
      wx.hideLoading();
      console.log(res);
      let contractNo = "parameter.contractNo";
      let contractType = "parameter.contractType";
      let contractProduct = "parameter.contractProduct";
      let depositPrice = "parameter.depositPrice";
      let contractPrice = "parameter.contractPrice";
      let realityPrice = "parameter.realityPrice";
      _this.setData({
        [contractNo]: res.contractNo,
        [contractType]: parseInt(res.contractType) - 1,
        [contractProduct]: res.contractProduct,
        [depositPrice]: res.depositPrice,
        [contractPrice]: res.contractPrice,
        [realityPrice]: res.realityPrice
      });
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.data.return_msg,
        icon: 'none',
        duration: 1000
      })
    })
  },
  submit: function () {
    if (this.data.parameter.contractNo.length < 1) {
      wx.showToast({
        title: "请输入合同编号",
        icon: 'none',
        duration: 1000
      })
      return
    }

    if (this.data.parameter.contractProduct.length < 1) {
      wx.showToast({
        title: "请输入合同产品",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.parameter.depositPrice.length < 1) {
      wx.showToast({
        title: "请输入合同产品",
        icon: 'none',
        duration: 1000
      })
      return
    }

    if (this.data.parameter.contractPrice.length < 1) {
      wx.showToast({
        title: "请输入合同产品",
        icon: 'none',
        duration: 1000
      })
      return
    }
    if (this.data.parameter.realityPrice.length < 1) {
      wx.showToast({
        title: "请输入合同产品",
        icon: 'none',
        duration: 1000
      })
      return
    }
    wx.showLoading({
      title: '更新合同中',
      mask: true,
    })

    let p = this.data.parameter;
    p.contractType = parseInt(p.contractType) + 1;
    Request.post('emCustomer/insertEmContract', p).then(res => {
      wx.hideLoading();
      wx.showToast({
        title: '更新成功',
        icon: 'none',
        duration: 1000
      })
      wx.navigateBack({});
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err.data.return_msg,
        icon: 'none',
        duration: 1000
      })
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let id = "parameter.customerId";
    this.setData({
      [id]: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})